#include "Entities/CircleEntity.h"
#include "Entities/BodyEntity.h"

using namespace cmn;
using namespace drawing;
using namespace editor_core;

CircleEntity::CircleEntity( BodyEntity* parentBody, const Circle& circle ) :
	m_circle( circle ),
	m_parentBody( parentBody )
{}

CircleEntity::~CircleEntity() {}

void CircleEntity::GetChildren( ICollection<IEntity*>& children ) const {
	children.Clear();
}

void CircleEntity::GetParents( ICollection<IEntity*>& parents ) const {
	parents.Clear();
	parents.Add( m_parentBody );
}

void CircleEntity::GetProperties( ICollection<Property>& properties ) const {
	properties.Clear();
	Property center( 201, "Center", 0 );
	center.AddChild( Property( 2011, "X", PropertyValue( m_circle.Center.X ) ) );
	center.AddChild( Property( 2012, "Y", PropertyValue( m_circle.Center.Y ) ) );
	properties.Add( center );
	properties.Add( Property( 202, "Radius", PropertyValue( m_circle.Radius ) ) );
}

bool CircleEntity::TryUpdateProperty( Property& property ) const {
	switch( property.GetKey() ) {
		case 201:
			return false;
		case 2011:
			property.SetValue( PropertyValue( m_circle.Center.X ) );
			return true;
		case 2012:
			property.SetValue( PropertyValue( m_circle.Center.Y ) );
			return true;
		case 202:
			property.SetValue( PropertyValue( m_circle.Radius ) );
			return true;
		default:
			return false;
	}
}

bool CircleEntity::TryUpdateFromProperty( const Property& property ) {
	switch( property.GetKey() ) {
		case 201:
			return false;
		case 2011:
			m_circle.Center.X = property.GetValue().GetFloat();
			return true;
		case 2012:
			m_circle.Center.Y = property.GetValue().GetFloat();
			return true;
		case 202:
			m_circle.Radius = property.GetValue().GetFloat();
			return true;
		default:
			return false;
	}
}

void CircleEntity::Tick() {}

void CircleEntity::DrawInner( IDrawer* drawer ) {
	drawer->DrawCircle( m_circle );
	float centerCrossRadius = 0.1f * m_circle.Radius;
	drawer->DrawLine( Vec2( m_circle.Center.X, m_circle.Center.Y - centerCrossRadius ), Vec2( m_circle.Center.X, m_circle.Center.Y + centerCrossRadius ) );
	drawer->DrawLine( Vec2( m_circle.Center.X - centerCrossRadius, m_circle.Center.Y ), Vec2( m_circle.Center.X + centerCrossRadius, m_circle.Center.Y ) );
}

Vec2 CircleEntity::Move( const Vec2& vector ) {
	m_circle.Center += vector;
	return vector;
}

void CircleEntity::Delete() {
	m_parentBody->RemoveCircle( this );
}

void CircleEntity::Undelete() {
	m_parentBody->AddCircle( this );
}

bool CircleEntity::Contains( const Vec2& point ) const {
	float dist = Distance( m_circle.Center, point );
	return dist <= m_circle.Radius;
}
